package com.acwer.framework.data;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import java.util.stream.Collectors;


import org.springframework.util.StringUtils;

import com.baomidou.mybatisplus.annotation.TableName;

import lombok.Data;

@Data
public class DataTable<T> {
	private List<DataColumn> columns;
	private List<T> rows;
	private Class<?> modelType;
	private String primaryKey;
	private String sourceTableName;
	
	private Map<String, String> searchCols=new HashMap<String, String>(){
		@Override
		public String toString() {
			return String.join("、", values());
		}
	};
	
	private String tableName;
	
	public DataTable(Class<?> clzz){
		modelType=clzz;
		columns=DataColumnUtils.getColumnsByClzz(clzz);
		
		//获取搜索字段
		columns.stream().filter(it->it.isSearch()).collect(Collectors.toList()).forEach(it->{
			searchCols.put(it.getColumnName(),it.getDisplay());
		});
		rows=new ArrayList<T>();
		//显示的table名称
		
		TableName table= clzz.getAnnotation(TableName.class);
		if(table!=null) tableName=table.value();
		//else tableName=clzz.getSimpleName();
		
//		Table tab=clzz.getAnnotation(Table.class);
		//实际的数据库的表名称
		TableName annoTable=clzz.getAnnotation(TableName.class);
		if(annoTable!=null) sourceTableName=annoTable.value();
		else sourceTableName=clzz.getSimpleName();
		
		if(StringUtils.isEmpty(tableName)&&annoTable!=null){
			
			tableName=annoTable.value(); 
		}
		
	}
//	public DataTable(DataRow row){
//		this(row.getModel().getClass());
//		rows.add(row);
//	}
	public DataTable(List<T> ls,Class<?> clzz){
		this(clzz);
		
		for (T t : ls) {
			rows.add(t);
		}
	}
	
}
